package o;

import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.HttpRetryException;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.SocketTimeoutException;
import java.security.cert.CertificateException;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSocketFactory;
import o.fuo.d;
import o.fus.a;
import okhttp3.Route;
import okhttp3.internal.connection.RouteException;
import okhttp3.internal.connection.StreamAllocation;
import okhttp3.internal.http2.ConnectionShutdownException;

public final class fvw implements fuo {
    private volatile StreamAllocation ˊ;
    private Object ˋ;
    private final boolean ˎ;
    private final fuv ˏ;
    private volatile boolean ॱ;

    public fvw(fuv o_fuv, boolean z) {
        this.ˏ = o_fuv;
        this.ˎ = z;
    }

    public void ˊ() {
        this.ॱ = true;
        StreamAllocation streamAllocation = this.ˊ;
        if (streamAllocation != null) {
            streamAllocation.cancel();
        }
    }

    public boolean ˎ() {
        return this.ॱ;
    }

    public void ˋ(Object obj) {
        this.ˋ = obj;
    }

    public StreamAllocation ॱ() {
        return this.ˊ;
    }

    public fvb ˎ(d dVar) throws IOException {
        boolean z;
        fus ˊ = dVar.ˊ();
        fvz o_fvz = (fvz) dVar;
        ftz ॱॱ = o_fvz.ॱॱ();
        fum ʽ = o_fvz.ʽ();
        StreamAllocation streamAllocation = new StreamAllocation(this.ˏ.ʻॱ(), ˎ(ˊ.ˏ()), ॱॱ, ʽ, this.ˋ, this.ˏ.ʼॱ());
        this.ˊ = streamAllocation;
        streamAllocation.address.ˎ(ˊ.ˋ("host"));
        int i = 0;
        StreamAllocation streamAllocation2 = streamAllocation;
        fus o_fus = ˊ;
        fvb o_fvb = null;
        while (!this.ॱ) {
            try {
                fvb ˊ2 = o_fvz.ˊ(o_fus, streamAllocation2, null, null);
                if (o_fvb != null) {
                    ˊ2 = ˊ2.ʽ().ˊ(o_fvb.ʽ().ॱ(null).ˏ()).ˏ();
                }
                try {
                    fus ˎ = ˎ(ˊ2, streamAllocation2.route());
                    if (ˎ == null) {
                        streamAllocation2.release();
                        return ˊ2;
                    }
                    fvg.ॱ(ˊ2.ᐝ());
                    int i2 = i + 1;
                    if (i2 > 20) {
                        streamAllocation2.release();
                        throw new ProtocolException("Too many follow-up requests: " + i2);
                    } else if (ˎ.ˎ() instanceof fwc) {
                        streamAllocation2.release();
                        throw new HttpRetryException("Cannot retry streamed HTTP body", ˊ2.ˊ());
                    } else {
                        if (!ॱ(ˊ2, ˎ.ˏ())) {
                            streamAllocation2.release();
                            streamAllocation = new StreamAllocation(this.ˏ.ʻॱ(), ˎ(ˎ.ˏ()), ॱॱ, ʽ, this.ˋ, this.ˏ.ʼॱ());
                            this.ˊ = streamAllocation;
                        } else if (streamAllocation2.codec() != null) {
                            throw new IllegalStateException("Closing the body of a  response didn't close its backing stream. Bad interceptor?");
                        } else {
                            streamAllocation = streamAllocation2;
                        }
                        i = i2;
                        streamAllocation2 = streamAllocation;
                        o_fus = ˎ;
                        o_fvb = ˊ2;
                    }
                } catch (IOException e) {
                    streamAllocation2.release();
                    throw e;
                }
            } catch (RouteException e2) {
                if (!ॱ(e2.ˎ(), streamAllocation2, false, o_fus)) {
                    throw e2.ˋ();
                }
            } catch (IOException e3) {
                if (e3 instanceof ConnectionShutdownException) {
                    z = false;
                } else {
                    z = true;
                }
                if (!ॱ(e3, streamAllocation2, z, o_fus)) {
                    throw e3;
                }
            } catch (Throwable th) {
                streamAllocation2.streamFailed(null);
                streamAllocation2.release();
            }
        }
        streamAllocation2.release();
        throw new IOException("Canceled");
    }

    private ftt ˎ(fur o_fur) {
        SSLSocketFactory ॱˊ;
        HostnameVerifier ͺ;
        fty o_fty = null;
        if (o_fur.ˎ()) {
            ॱˊ = this.ˏ.ॱˊ();
            ͺ = this.ˏ.ͺ();
            o_fty = this.ˏ.ˏॱ();
        } else {
            ͺ = null;
            ॱˊ = null;
        }
        return new ftt(o_fur.ʻ(), o_fur.ॱॱ(), this.ˏ.ʼ(), this.ˏ.ˋॱ(), ॱˊ, ͺ, o_fty, this.ˏ.ॱˋ(), this.ˏ.ʽ(), this.ˏ.ʽॱ(), this.ˏ.ʿ(), this.ˏ.ᐝ());
    }

    private boolean ॱ(IOException iOException, StreamAllocation streamAllocation, boolean z, fus o_fus) {
        streamAllocation.streamFailed(iOException);
        if (!this.ˏ.ᐝॱ()) {
            return false;
        }
        if ((!z || !(o_fus.ˎ() instanceof fwc)) && ˎ(iOException, z) && streamAllocation.hasMoreRoutes()) {
            return true;
        }
        return false;
    }

    private boolean ˎ(IOException iOException, boolean z) {
        boolean z2 = true;
        if (iOException instanceof ProtocolException) {
            return false;
        }
        if (iOException instanceof InterruptedIOException) {
            if (!(iOException instanceof SocketTimeoutException) || z) {
                z2 = false;
            }
            return z2;
        } else if (((iOException instanceof SSLHandshakeException) && (iOException.getCause() instanceof CertificateException)) || (iOException instanceof SSLPeerUnverifiedException)) {
            return false;
        } else {
            return true;
        }
    }

    private fus ˎ(fvb o_fvb, Route route) throws IOException {
        fuw o_fuw = null;
        if (o_fvb == null) {
            throw new IllegalStateException();
        }
        int ˊ = o_fvb.ˊ();
        String ˋ = o_fvb.ॱ().ˋ();
        switch (ˊ) {
            case 300:
            case 301:
            case 302:
            case 303:
                break;
            case 307:
            case 308:
                if (!(ˋ.equals(HttpContants.HTTP_METHOD_GET) || ˋ.equals("HEAD"))) {
                    return null;
                }
            case 401:
                return this.ˏ.ˊॱ().ॱ(route, o_fvb);
            case 407:
                Proxy proxy;
                if (route != null) {
                    proxy = route.proxy();
                } else {
                    proxy = this.ˏ.ʽ();
                }
                if (proxy.type() == Type.HTTP) {
                    return this.ˏ.ॱˋ().ॱ(route, o_fvb);
                }
                throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
            case 408:
                if (!this.ˏ.ᐝॱ() || (o_fvb.ॱ().ˎ() instanceof fwc)) {
                    return null;
                }
                if ((o_fvb.ˋॱ() == null || o_fvb.ˋॱ().ˊ() != 408) && ˎ(o_fvb, 0) <= 0) {
                    return o_fvb.ॱ();
                }
                return null;
            case ResponseBean.SERVER_DELAY_RETRY /*503*/:
                if ((o_fvb.ˋॱ() == null || o_fvb.ˋॱ().ˊ() != ResponseBean.SERVER_DELAY_RETRY) && ˎ(o_fvb, (int) ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED) == 0) {
                    return o_fvb.ॱ();
                }
                return null;
            default:
                return null;
        }
        if (!this.ˏ.ॱˎ()) {
            return null;
        }
        String ˋ2 = o_fvb.ˋ("Location");
        if (ˋ2 == null) {
            return null;
        }
        fur ˊ2 = o_fvb.ॱ().ˏ().ˊ(ˋ2);
        if (ˊ2 == null) {
            return null;
        }
        if (!ˊ2.ˋ().equals(o_fvb.ॱ().ˏ().ˋ()) && !this.ˏ.ॱᐝ()) {
            return null;
        }
        a ʽ = o_fvb.ॱ().ʽ();
        if (fvt.ˏ(ˋ)) {
            boolean ॱ = fvt.ॱ(ˋ);
            if (fvt.ˊ(ˋ)) {
                ʽ.ˎ(HttpContants.HTTP_METHOD_GET, null);
            } else {
                if (ॱ) {
                    o_fuw = o_fvb.ॱ().ˎ();
                }
                ʽ.ˎ(ˋ, o_fuw);
            }
            if (!ॱ) {
                ʽ.ˏ("Transfer-Encoding");
                ʽ.ˏ("Content-Length");
                ʽ.ˏ(HttpContants.KEY_CONTENT_TYPE);
            }
        }
        if (!ॱ(o_fvb, ˊ2)) {
            ʽ.ˏ("Authorization");
        }
        return ʽ.ˊ(ˊ2).ॱ();
    }

    private int ˎ(fvb o_fvb, int i) {
        String ˋ = o_fvb.ˋ("Retry-After");
        if (ˋ == null) {
            return i;
        }
        if (ˋ.matches("\\d+")) {
            return Integer.parseInt(ˋ);
        }
        return ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
    }

    private boolean ॱ(fvb o_fvb, fur o_fur) {
        fur ˏ = o_fvb.ॱ().ˏ();
        return ˏ.ʻ().equals(o_fur.ʻ()) && ˏ.ॱॱ() == o_fur.ॱॱ() && ˏ.ˋ().equals(o_fur.ˋ());
    }
}
